<template>
  <header class="header">
    <div class="header-nav app-main">
      <div class="lf">
        <div class="logo">
          <img src="@/assets/index/logo.png" alt="" />
        </div>
        <div class="nav">
          <div
            class="li"
            v-for="(item, index) in navList"
            :key="index"
            @click="$router.push('/work')"
          >
            {{ item }}
          </div>
        </div>
      </div>
      <el-popover v-if="userInfo.username" class="popover">
        <div class="user-options">
          <div class="option" @click="$router.push('/')">
            <span class="icon"> <svg-icon iconClass="work"></svg-icon></span>
            <span>作品管理</span>
          </div>
          <div class="option logout" @click="logout">
            <i class="el-icon-switch-button"></i> <span>退出账号</span>
          </div>
        </div>
        <div class="user" slot="reference">
          <img
            :src="
              'http://116.205.104.204:8080/api/v1/creations/' + userInfo.avatar
            "
            class="avatar"
            alt="Avatar"
            v-if="userInfo.avatar"
          />
          <img
            src="@/assets/images/profile.jpg"
            class="avatar"
            alt="Avatar"
            v-else
          />
          <div class="username">{{ userInfo.username }}</div>
        </div>
      </el-popover>

      <div class="auth-buttons" v-else>
        <el-button type="primary" round @click="$router.push('/register')"
          >注册</el-button
        >
        <el-button round @click="$router.push('/login')">登录</el-button>
      </div>
    </div>
  </header>
</template>
<script>
import { mapGetters } from "vuex";
import { removeToken } from "@/utils/auth";
export default {
  computed: {
    ...mapGetters(["userInfo"]),
  },
  data() {
    return {
      navList: ["产品介绍", "使用文档"],
    };
  },
  methods: {
    async logout() {
      this.$confirm("确定要退出账号吗？", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          removeToken();
          location.href = "/login";
        })
        .catch(() => {});
    },
  },
};
</script>
<style lang="scss" scoped>
.app-main {
  max-width: 1400px;
  margin: auto;
  padding: 0 40px;
}
.header {
  background: #fff;
  position: sticky;
  top: 0;
  left: 0;
  z-index: 11;
  .header-nav {
    display: flex;
    justify-content: space-between;
    align-items: center;
    height: 64px;
  }

  .lf {
    display: flex;
    align-items: center;

    .logo {
    }

    .nav {
      display: flex;
      align-items: center;

      .li {
        margin-left: 48px;
        font-weight: 500;
        font-size: 16px;
        color: #00011b;
        cursor: pointer;
      }

      .li:hover {
        color: #165deb;
      }
    }
  }

  .nav-menu {
    border: none;
  }

  .user {
    display: flex;
    align-items: center;
    gap: 10px;
    cursor: pointer;
    .avatar {
      width: 36px;
      height: 36px;
      border-radius: 50%;
      object-fit: cover;
    }
    .username {
      font-weight: bold;
      font-size: 14px;
      color: #00011b;
      line-height: 16px;
      white-space: nowrap; /* 防止文本换行 */
      overflow: hidden; /* 隐藏溢出的内容 */
      text-overflow: ellipsis; /* 显示省略号 */
      padding-right: 5px;
    }
  }
}
.user-options {
  width: 180px;
  box-sizing: border-box;
  margin: -10px;
  .option {
    display: flex;
    align-items: center;
    cursor: pointer;
    height: 42px;
    line-height: 42px;
    padding: 10px;
    font-size: 14px;
    color: #00011b;
    box-sizing: border-box;
    i,
    .icon {
      background: rgba(0, 1, 27, 0.1);
      border-radius: 50%;
      height: 24px;
      width: 24px;
      text-align: center;
      line-height: 24px;
      margin-right: 8px;
      .svg-icon {
        width: 16px;
        height: 16px;
      }
    }
  }

  .option.logout i {
    background: rgba(236, 17, 17, 0.1);
  }

  .option:hover {
    background: #f2f4f8;
  }

  .logout {
    color: #ec1111;
  }
}
</style>
